import java.util.ArrayList;
import java.util.List;

class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    int sum = 0;
    int t = 0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        t = target;

        dfs(candidates, 0);
        return ret;
    }
    public void dfs(int[] candidates, int pos){
        if(sum > t || pos >= candidates.length) return;
        else if(sum == t){
            ret.add(new ArrayList<>(path));
            return;
        }
        for(int i = pos; i < candidates.length; i++){
            path.add(candidates[i]);
            sum += candidates[i];
            dfs(candidates, i);
            // 回溯
            path.remove(path.size() - 1);
            sum -= candidates[i];
        }
    }
}